package com.soft.javase.day10.consolidation.reduce;

import java.util.Optional;
import java.util.stream.Stream;

/**
 * @author yzg
 * @desc TODO
 * @date 2025/5/4
 */
public class ReduceDemo {
    public static void main(String[] args) {
        //求一组数据中的最大值
        Optional<Integer> optional = Stream.of(10, 30, 59, 66).reduce(Integer::max);
        System.out.println(optional.orElse(0));

        //求一组数据的和
        Optional<Integer> optional1 = Stream.of(1, 3, 7, 9).reduce(Integer::sum);
        System.out.println(optional1.orElse(0));

        //求一组数据的拼接
        Optional<String> optional2 = Stream.of("a", "b", "c").reduce(String::concat);
        System.out.println(optional2.orElse(""));

        //求一组员工的工资总和
        Employee emp1 = new Employee("张三", 6000.0);
        Employee emp2 = new Employee("李四", 7000.0);
        Employee emp3 = new Employee("王五", 8000.0);
        Employee emp4 = new Employee("赵六", 9000.0);

        Optional<Double> optional3 = Stream.of(emp1, emp2, emp3, emp4).map(Employee::getSalary).reduce(Double::sum);
        System.out.println(optional3.orElse(0.0));

    }
}
